﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CosmosKernel4.Install
{
    class plasmidInstall
    {
        public void Install()
        {
            Console.ForegroundColor = ConsoleColor.White;
            Console.Clear();
            
            Console.WriteLine("Welcome to the Plasmid OS installer");
            Console.WriteLine("Pick a partition to install onto");
            for (int i = 0; i < Cosmos.Hardware.BlockDevice.Partition.Devices.Count; i++)
            {
                Console.WriteLine((i + 1).ToString() + ": Size: " + (Cosmos.Hardware.BlockDevice.Partition.Devices[i].BlockCount).ToString());
            }
            
            String a = Console.ReadLine();
            Cosmos.Hardware.BlockDevice.Partition part = (Cosmos.Hardware.BlockDevice.Partition)Cosmos.Hardware.BlockDevice.Partition.Devices[Int32.Parse(a) - 1];
            Console.WriteLine("Plasmid will now format your chosen partition. Press any key to continue.");
            GruntyOS.HAL.GLNFS fs = new GruntyOS.HAL.GLNFS(part);
            
            fs.Format("Plasmid");
            Console.WriteLine("Format complete.");
            Console.Clear();
            Console.WriteLine("Plasmid installer part 2 - setup");
            Console.WriteLine("Enter a password for the administrator account, 'root'");
            byte[] pass = new Plasmid.Crypto.MD5.MD5().ComputeHash(CosmosKernel4.Global.StringManip.GetBytes(Console.ReadLine()));
            string hash = CosmosKernel4.Global.StringManip.stripchar(BitConverter.ToString(pass).ToLowerInvariant(), '-');
            string passwdf = "root:"+hash+":0";
            Console.WriteLine("Your computer needs a name! Please enter it now");
            string name = Console.ReadLine();
            Console.WriteLine("Assembling Plasmid system files, please wait");
            GruntyOS.CurrentUser.Username = "SYSTEM";
            GruntyOS.CurrentUser.Privilages = 0;
            fs.makeDir("/plasmid", "SYSTEM");
            fs.saveFile(CosmosKernel4.Global.StringManip.GetBytes(passwdf), "/plasmid/passwd", "SYSTEM");
            fs.saveFile(CosmosKernel4.Global.StringManip.GetBytes(name), "/plasmid/compname", "SYSTEM");
            fs.makeDir("/root", "root");

            Console.WriteLine("Plasmid install complete. Press any key to reboot...");
            Console.ReadKey();
            
            CosmosKernel4.PowerManagement.PowerMgr pmg = new CosmosKernel4.PowerManagement.PowerMgr();
            pmg.reboot();
                 


            

        }
    }
}
